package com.itboye.lottery.fragment;


import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.lottery.base.BaseFragment;
import com.itboye.lottery.R;
import com.itboye.lottery.activity.Dlt_SsqPrizeDetailActivity;
import com.itboye.lottery.adapter.AnnouncementAdapter;
import com.itboye.lottery.alert.ByAlert;
import com.itboye.lottery.bean.NoticeBean;
import com.itboye.lottery.enums.LotteryType;
import com.itboye.lottery.presenter.UserPresenter;
import com.itboye.lottery.retrofit.ResponseCallback;
import com.itboye.lottery.widget.titlebarwidget.TranslucentActionBar;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/11/10.
 */

public class AnnouncementFragment extends BaseFragment implements ResponseCallback {

    TextView textView;
    TranslucentActionBar transaction_bar;
    RecyclerView rv_fragment_find;
    private AnnouncementAdapter adapter;
    UserPresenter userPresenter = new UserPresenter(this);
    private ArrayList<NoticeBean.NoticeItem> noticeItemArrayList = new ArrayList<>();
    LinearLayout ll_announcement_detail_number;
    private boolean isInit;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_find;
    }

    @Override
    protected void initData() {
        transaction_bar.getTvTitle().setText(getString(R.string.announcement_prize));
        rv_fragment_find.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new AnnouncementAdapter(this, noticeItemArrayList);
        adapter.setOnItemClickListener((view, position) -> {
            String cateId = noticeItemArrayList.get(position).getCategory_id();
            if (cateId != null) {
                switch (cateId) {
                    case "1001"://双色球
                        startActivity(new Intent(getActivity(), Dlt_SsqPrizeDetailActivity.class)
                                .putExtra("type", LotteryType.SSQ)
                                .putExtra("id", noticeItemArrayList.get(position).getId()));//双色球 双色球开奖详情
                        break;
                    case "2001"://大乐透
                        startActivity(new Intent(getActivity(), Dlt_SsqPrizeDetailActivity.class)
                                .putExtra("type", LotteryType.DLT)
                                .putExtra("id", noticeItemArrayList.get(position).getId()));//大乐透 双色球开奖详情
                        break;
                    case "0"://足球
                    case "3010"://足球
                        startActivity(new Intent(getActivity(), Dlt_SsqPrizeDetailActivity.class)
                                .putExtra("type", LotteryType.JCZQ)
                                .putExtra("open_time", noticeItemArrayList.get(position).getOpen_time())
                                .putExtra("ball_type", noticeItemArrayList.get(position).getBall_type()));//足球 双色球开奖详情
                        break;
                    case "3001"://蓝球
                        startActivity(new Intent(getActivity(), Dlt_SsqPrizeDetailActivity.class)
                                .putExtra("type", LotteryType.JCLQ)
                                .putExtra("ball_type", noticeItemArrayList.get(position).getBall_type())
                                .putExtra("open_time", noticeItemArrayList.get(position).getOpen_time())
                                .putExtra("id", noticeItemArrayList.get(position).getId()));//蓝球 双色球开奖详情
                        break;
                }
            }

            //足球篮球开奖详情
        });
        rv_fragment_find.setAdapter(adapter);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser&&isInit) {
            isInit=true;
            getAnnouncementData();
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    private void getAnnouncementData() {
        userPresenter.getNotice();
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void success(@NotNull Object o, @NotNull String requestSuccessType) {
        if (requestSuccessType == userPresenter.getNotice_SUCCESS) {
            NoticeBean noticeBean = (NoticeBean) o;
            noticeItemArrayList.addAll(noticeBean.getData());
            adapter.notifyDataSetChanged();
        } else {

        }
    }

    @Override
    public void fail(int code, @NotNull Object msg, @NotNull String requestErrorType) {
        if (requestErrorType == userPresenter.getNotice_FAIL) {
            ByAlert.alert(msg);
        } else {

        }
    }
}
